//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTask;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTaskListener;

public class XThreadTask implements IXThreadTask {
    private Thread mThread = null;
    private Runnable mRunnable = null;
    private Handler mHandler = null;
    private IXThreadTaskListener mIXThreadTaskListener = null;
    private boolean mWork = false;
    private Object mObjLock = null;
    private static final int VALUE_INT_MESSAGEID_THREAD_TASK = 4096;

    public XThreadTask() {
        this._init();
    }

    private void _init() {
        this.mObjLock = new Object();
        this.mHandler = new Handler(Looper.getMainLooper()) {
            public void handleMessage(Message msg) {
                if (4096 == msg.what) {
                    if (XThreadTask.this.mIXThreadTaskListener != null) {
                        XThreadTask.this.mIXThreadTaskListener.onThreadComplete();
                    }

                    XThreadTask.this.clear();
                }

            }
        };
        this.mRunnable = new Runnable() {
            public void run() {
                if (XThreadTask.this.mIXThreadTaskListener != null) {
                    XThreadTask.this.mIXThreadTaskListener.onThreadWork();
                }

                Message msg = new Message();
                msg.what = 4096;
                XThreadTask.this.mHandler.sendMessage(msg);
            }
        };
    }

    public boolean start(IXThreadTaskListener iXThreadTaskListener) {
        Object var2 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (!this.mWork && iXThreadTaskListener != null) {
                this.mWork = true;
                this.mIXThreadTaskListener = iXThreadTaskListener;
                this.mThread = new Thread(this.mRunnable);
                this.mThread.start();
                return true;
            } else {
                return false;
            }
        }
    }

    public void stop() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (this.mWork && this.mThread != null) {
                this.mThread.interrupt();
            }
        }

        this.clear();
    }

    public boolean isWork() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            return this.mWork;
        }
    }

    private void clear() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            this.mWork = false;
            this.mIXThreadTaskListener = null;
            this.mThread = null;
        }
    }
}
